//
//  BaseRNViewController.m
//  MDLoanRN
//
//  Created by kaelzhou on 17/2/27.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "WFRViewController.h"
#import <React/RCTBundleURLProvider.h>
#import "WFRViewLifeCycleModule.h"
#import "WFReactBridge.h"
#import <React/RCTRootView.h>
#import <WFFoundation/FDFullscreenPopGesture.h>

@interface WFRViewController ()

@end

@implementation WFRViewController

- (instancetype)initWithURL:(NSString *)URLString module:(NSString *)module userInfo:(NSDictionary *)userInfo{
    if (self = [self init]) {
        _moduleName = module;
        _userInfo = userInfo;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(javaScriptDidFailToLoad)
                                                     name:RCTJavaScriptDidFailToLoadNotification
                                                   object:nil];
        [self loadModule];
    }
    return self;
}

- (void)dealloc
{
    [WFRViewLifeCycleModule sendDeallocEvent:self.rctRootView.moduleName];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
}

- (void)javaScriptDidFailToLoad {
    [self loadModule];
}

- (void)loadModule {
    if (self.moduleName.length > 0){
        [self.rctRootView removeFromSuperview];
        RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:[WFReactBridge manager].bridge
                                                         moduleName:self.moduleName
                                                  initialProperties:self.userInfo];
        rootView.frame = self.view.bounds;
        rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

        [self.view addSubview:rootView];
        self.rctRootView = rootView;
    }
}

+ (BOOL)isStatisticPV{
    return NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [WFRViewLifeCycleModule sendViewWillAppearEvent:self.rctRootView.moduleName];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [WFRViewLifeCycleModule sendViewWillDisappearEvent:self.rctRootView.moduleName];
}

#pragma mark - Override
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
